from myProfile.models.data.myProfile_models import Login, Medication
from typing import List


class MedicationRepository:

    def __init__(self, engine):
        self.engine = engine

    async def add_to_profile(
            self, login_id: int, medication: Medication) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == login_id)
            medications = login.profiles.medications + [medication]
            login.profiles.medications = medications
            await self.engine.save(login)
        except Exception as e:
            print(e)
            return False
        return True

    async def remove_from_profile(self, login_id: int, id: int) -> bool:
        try:
            login = await self.engine.find_one(Login, Login.id == login_id)
            medications = login.profiles.medications
            values = [value for value in medications if value.id != id]
            login.profiles.medications = values
            await self.engine.save(login)
        except Exception as e:
            print(e)
            return False
        return True

    async def get_from_profile(self, login_id: int) -> List[Medication]:
        login = await self.engine.find_one(Login, Login.id == login_id)
        return login.profiles.medications
